package com.atguigu.lease.web.admin.service.Impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 李壮
 * @CreateTime: 2025-05-07
 * @Version: 1.0
 */

@Service
public class ApartmentServiceImpl extends ServiceImpl<ApartmentMapper, ApartmentInfo> implements ApartmentService {

    @Resource
    ApartmentMapper apartmentMapper;

    @Autowired
    ApartmentFacilityService apartmentFacilityService;

    @Autowired
    ApartmentLabelService apartmentLabelService;

    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    ProvinceService provinceService;

    @Autowired
    CityService cityService;

    @Autowired
    DistrictService districtService;

    @Autowired
    GraphInfoService graphInfoService;

    @Resource
    FacilityMapper facilityMapper;

    @Resource
    LabelInfoMapper labelInfoMapper;

    @Resource
    FeeValueMapper feeValueMapper;

    @Resource
    GraphInfoMapper graphInfoMapper;

    @Resource
    RoomInfoMapper roomInfoMapper;

    //保存或更新公寓信息
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {


        if (apartmentSubmitVo.getId() != null){

            //1.删除配套信息
            LambdaQueryWrapper<ApartmentFacility> facilitylambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilitylambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            apartmentFacilityService.remove(facilitylambdaQueryWrapper);

            //2.删除标签信息
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

            //3.删除杂费信息
            LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);

            //4.删除图片列表
            LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoQueryWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            graphInfoService.remove(graphInfoQueryWrapper);
        }

        //表单只提交id这里需要先根据省市区id查找名字填进去
        apartmentSubmitVo.setProvinceName(provinceService.getById(apartmentSubmitVo.getProvinceId()).getName());
        apartmentSubmitVo.setCityName(cityService.getById(apartmentSubmitVo.getCityId()).getName());
        apartmentSubmitVo.setDistrictName(districtService.getById(apartmentSubmitVo.getDistrictId()).getName());

        //先保存公寓数据 然后会返回id 再进行操作
        super.saveOrUpdate(apartmentSubmitVo);

        //然后再添加新数据
        //1.添加配套
        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIdList)){
            ArrayList<ApartmentFacility> list = new ArrayList<>();
            for (Long facilityId : facilityInfoIdList) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .facilityId(facilityId)
                        .build();

                list.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(list);
        }

        //2.添加标签
        List<Long> labelIdList = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIdList)){
            ArrayList<ApartmentLabel> list = new ArrayList<>();
            for (Long labelId : labelIdList) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .labelId(labelId)
                        .build();
                list.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(list);
        }

        //3.添加杂费
        List<Long> feeValueIdsList = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIdsList)){
            ArrayList<ApartmentFeeValue> list = new ArrayList<>();
            for (Long feeValueId : feeValueIdsList) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .feeValueId(feeValueId)
                        .build();
                list.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(list);
        }

        //4.添加图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> list = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                list.add(graphInfo);
            }
            graphInfoService.saveBatch(list);
        }
    }

    //根据条件分页查询公寓列表
    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo apartmentQueryVo) {
        return apartmentMapper.pageApartmentItemByQuery(page,apartmentQueryVo);
    }


    //根据ID获取公寓详细信息
    @Override
    public ApartmentDetailVo getApartmentDetailByid(Long id) {

       ApartmentInfo apartmentInfo = this.getById(id);
       if (apartmentInfo == null) return null;

       ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

        //查询关联数据
        List<FacilityInfo> facilityInfoList = facilityMapper.selectListByApartmentId(id);

        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);

        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT,id);

        //封装查询后的结果并返回
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setGraphVoList(graphVoList);

        return apartmentDetailVo;
    }

    //根据id删除公寓信息
    @Override
    public void removeAparymentById(Long id) {


        //判断一下该公寓下是否存在房间信息
        Long count = roomInfoMapper.selectCount(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
        if (count>0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //删除主表数据
        super.removeById(id);

        //1.删除配套信息
        LambdaQueryWrapper<ApartmentFacility> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(lambdaQueryWrapper);

        //2.删除标签信息
        LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(labelLambdaQueryWrapper);

        //3.删除杂费信息
        LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);

        //删除图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);




    }


}














